import sys
import time
import json
from datetime import datetime
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.chrome.options import Options
import urllib.parse
from mongoengine import *


class privateuser(Document):
    nickname = StringField()
    gender = IntField()
    city = StringField()
    followerCount = IntField()
    uid = StringField()
    addTime = DateTimeField()
    accountUid = StringField()
    accountName = StringField()
    updatetime = DateTimeField()
    postCount = IntField()
    fansCount = IntField()
    searched = IntField()
    privateList = ListField()
    searched = IntField()
    isFans = IntField()
    isClicked = IntField()
    isDelete = IntField()
    meta = {
        'strict':False
    }


class newprivateuser(Document):
    nickname = StringField()
    gender = IntField()
    city = StringField()
    followerCount = IntField()
    uid = StringField()
    addTime = DateTimeField()
    accountUid = StringField()
    accountName = StringField()
    updatetime = DateTimeField()
    postCount = IntField()
    fansCount = IntField()
    searched = ListField()
    firstClickUser = ListField()
    allClickUser = ListField()
    isClicked = IntField()
    isDelete = IntField()
    meta = {
        'strict':False
    }

class myprivateaccounts(Document):
    name = StringField()
    uid = StringField()
    addTime = DateTimeField()
    updatetime = DateTimeField()
    isDelete = IntField()
    meta = {
        'strict':False
    }


def NewDriver():
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=" + path2 + 'userlist' + accountName)
    driver = webdriver.Chrome(path, options=chrome_options)
    driver.maximize_window()
    return driver


def userlistDriver(accountName):
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=" + path2 + 'userlist' + accountName)
    driver = webdriver.Chrome(path, options=chrome_options)
    driver.maximize_window()
    return driver


def searchprivateDriver(accountName):
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=" + path2 + 'searchprivate' + accountName)
    driver = webdriver.Chrome(path, options=chrome_options)
    driver.maximize_window()
    return driver




if __name__ == '__main__':
    index = int(sys.argv[1])
    # accountName = sys.argv[1]
    # accountUid = sys.argv[2]
    mode = 1
    path = 'd:/work/weiboprivate/python/chromedriver.exe'
    path2 = 'd:/work/weiboprivate/profile/'
    if mode == 1:
        path = 'd:/weiboprivate/python/chromedriver.exe'
        path2 = 'd:/weiboprivate/profile/'
        connect('weibo', host='1.15.92.12', port=27817)
        # connect('weibo', host='localhost', port=27017)
    else:
        connect('weibo', host='1.15.92.12', port=27817)
        # connect('weibo', host='localhost', port=27017)

    accountlist = myprivateaccounts.objects(isDelete=0)
    print(accountlist[index])
    # for account in accountlist:
    #     userlistDriver(account['name']).get("https://www.weibo.com")
    #     searchprivateDriver(account['name']).get("https://www.weibo.com")
    account = accountlist[index]
    # userlistDriver(account['name']).get("https://www.weibo.com")
    searchprivateDriver(account['name']).get("https://www.weibo.com")
    while True:
        print("wating login",account['name'])
        time.sleep(10)
    # driver = NewDriver()
    # fansUrl = "https://weibo.com/{UID}/fans?cfs=600&relate=fans&t=1&f=1&type=&Pl_Official_RelationFans__88_page={page}#Pl_Official_RelationFans__88"
    # driver.get(fansUrl.format(UID=accountUid, page=1))
    # time.sleep(2)
    # getFans(driver,accountUid,accountName)
    # driver.quit()